package routers

import (
	"github.com/gin-gonic/gin"
	swaggerFiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
	api "go-mall/api/v1"
	"go-mall/middleware"
	"net/http"
)

// gin-swagger middleware
// swagger embed files

func NewRouter() *gin.Engine {
	r := gin.Default()
	r.Use(middleware.Cors())
	r.StaticFS("/static", http.Dir("/static"))

	v1 := r.Group("api/v1")
	{

		v1.GET("ping", func(c *gin.Context) {
			c.JSON(200, "success")
		})
		//用户操作
		v1.POST("user/register", api.UserRegister)
		v1.POST("user/login", api.UserLogin)
		//轮播图
		v1.GET("common/carousels", api.ListCarousel)
		//获取全部分类
		v1.GET("common/categories", api.ListCategories)
		//商品操作
		v1.POST("product/list", api.ListProduct)
		v1.POST("product/search", api.SearchProduct)
		v1.GET("product/:id", api.ShowProduct)
		//	需要登录保护的
		authed := v1.Group("/")
		authed.Use(middleware.JWT())
		{
			//	用户操作
			authed.PUT("user", api.UserUpdate)
			authed.POST("user/avatar", api.UploadAvatar)
			authed.POST("user/sending-email", api.SendEmail)
			authed.POST("user/valid-email", api.ValidEmail)
			authed.POST("user/info", api.UserInfo)

			//轮播图

			//	显示金额
			authed.POST("money", api.ShowMoney)

			//商品操作
			authed.POST("product/create", api.CreateProduct)
			authed.GET("categories", api.ListCategory)

			//	收藏夹操作
			authed.GET("favorites", api.ListFavorite)
			authed.POST("favorites", api.CreateFavorite)
			authed.DELETE("favorites/:id", api.DeleteFavorite)

			//	地址操作
			authed.POST("address", api.CreateAddress)
			authed.GET("address/:id", api.GetAddress)
			authed.GET("address", api.ListAddress)
			authed.PUT("address/:id", api.UpdateAddress)
			authed.DELETE("address/:id", api.DeleteAddress)

			//	购物车操作
			authed.POST("carts", api.CreateCart)
			authed.GET("carts", api.ListCart)
			authed.PUT("carts/:id", api.UpdateCart)
			authed.DELETE("carts/:id", api.DeleteCart)

			//	订单操作
			authed.POST("orders", api.CreateOrder)
			authed.GET("orders", api.ListOrder)
			//authed.GET("orders/:id", api.ShowOrder)
			//authed.DELETE("orders/:id", api.DeleteOrder)

			//authed.GET("pay", api.OrderPay)
			//	余额支付
			authed.POST("pay/balance", api.OrderPay)

			//	通知
			authed.GET("notification", api.ListNotification)

			//	上传图片
			authed.POST("upload", api.UploadImg)

		}
	}
	r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
	return r
}
